package com.tian.hello.ui.qqfriend;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.tian.hello.R;

import java.util.ArrayList;

/**
 * Description:
 * Author：skyTian
 * Time:2018/2/2
 * CopyRight:com,sky
 */
public class ExListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private ArrayList<ExListInfo> exListInfos;
    private LayoutInflater inflater;


    public ExListAdapter(Context context, ArrayList<ExListInfo> exListInfos) {
        this.context = context;
        this.exListInfos = exListInfos;
        this.inflater = ((AppCompatActivity) context).getLayoutInflater();
    }

    @Override
    public int getGroupCount() {
        return exListInfos.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return exListInfos.get(groupPosition).getDatas().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return exListInfos.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return exListInfos.get(groupPosition).getDatas().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_exlist_topview, parent,false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tv = convertView.findViewById(R.id.id_num);
            convertView.setTag(groupViewHolder);
        }
        //
        else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tv.setText(exListInfos.get(groupPosition).getName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_exlist_childview, null);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tv = convertView.findViewById(R.id.id_num);
            convertView.setTag(childViewHolder);
        }
        //
        else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        ExListInfo.ExChildItem cItem = exListInfos.get(groupPosition).getDatas().get(childPosition);

        childViewHolder.tv.setText(cItem.getName());
        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    static class GroupViewHolder {
        TextView tv;
    }

    static class ChildViewHolder {
        TextView tv;
    }
}
